Imports System
Imports System.Data
Imports System.Collections
Imports System.Web.UI
Imports Telerik.Web.UI
Partial Class WebUserControl
    Inherits System.Web.UI.UserControl
    Public Event SelectedIndexChanged(ByVal o As Object, ByVal e As Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs)

    Public Enum DropDownDefaultItem
        ChooseOne
        ChooseAll
    End Enum

    Public Enum DropDownNothingItem
        ChooseNothing
    End Enum

    Protected Overridable Sub OnSelectedIndexChanged(ByVal e As System.EventArgs)
        RaiseEvent SelectedIndexChanged(Me, e)
    End Sub

    Public Property DataSourceId() As String
        Get
            Return rcbv.DataSourceID
        End Get
        Set(ByVal value As String)
            rcbv.DataSourceID = value
            rcbd.DataSourceID = value
        End Set
    End Property

    <ComponentModel.Browsable(False)> _
    Public Property SelectedValue() As String
        Get
            Return IIf(rcbv.SelectedValue = "-1", "", rcbv.SelectedValue)
        End Get
        Set(ByVal value As String)

            If rcbv.FindItemByValue(value) Is Nothing Or value = "-2" Then
                rcbv.SelectedValue = "-1"
                rcbd.SelectedValue = "-1"
            Else
                rcbv.SelectedValue = value
                rcbd.SelectedValue = value
            End If
        End Set
    End Property

    <ComponentModel.Browsable(False)> _
    Public ReadOnly Property SelectedText() As String
        Get
            Return rcbd.SelectedItem.Text
        End Get
    End Property

    <ComponentModel.Browsable(False)> _
    Public Property SelectedIndex() As Integer
        Get
            Return rcbv.SelectedIndex
        End Get
        Set(ByVal value As Integer)
            rcbv.SelectedIndex = value
            rcbd.SelectedIndex = value
        End Set
    End Property

    Public Property DataValueField() As String
        Get
            Return rcbv.DataValueField
        End Get
        Set(ByVal value As String)

            rcbv.DataValueField = value
            rcbv.DataTextField = value

            rcbd.DataValueField = value
        End Set
    End Property

    Public Property DataTextField() As String
        Get
            Return rcbd.DataTextField
        End Get
        Set(ByVal value As String)
            rcbd.DataTextField = value
        End Set
    End Property

    Public WriteOnly Property DefaultForeColor() As String
        Set(ByVal value As String)
            If Not rcbv.FindItemByValue(value) Is Nothing Then
                rcbv.FindItemByValue(value).ForeColor = Drawing.Color.Red
                rcbd.FindItemByValue(value).ForeColor = Drawing.Color.Red
            End If
        End Set
    End Property

    Public Property AutoPostBack() As Boolean
        Get
            Return rcbv.AutoPostBack
        End Get
        Set(ByVal value As Boolean)
            rcbv.AutoPostBack = value
            'rcbd.AutoPostBack = value
        End Set
    End Property

    Public Property NoWrap() As Boolean
        Get
            Return rcbv.NoWrap
        End Get
        Set(ByVal value As Boolean)
            rcbv.NoWrap = value
            rcbd.NoWrap = value
        End Set
    End Property

    Public Property IsRequired() As Boolean
        Get
            Return CVrcbv.Enabled
        End Get
        Set(ByVal value As Boolean)
            CVrcbv.Enabled = value
        End Set
    End Property

    Public Property ValidationGroup() As String
        Get
            Return CVrcbv.ValidationGroup
        End Get
        Set(ByVal value As String)
            CVrcbv.ValidationGroup = value
            rcbv.ValidationGroup = value
        End Set
    End Property

    'Amer 
    '14/02/2007
    Public Property Enabled() As Boolean
        Get
            Return rcbv.Enabled
        End Get
        Set(ByVal value As Boolean)
            rcbv.Enabled = value
            rcbd.Enabled = value
        End Set
    End Property

    Public WriteOnly Property DefaultChoosedItem() As DropDownDefaultItem
        Set(ByVal value As DropDownDefaultItem)

            Dim strDefaultText As String
            If value = DropDownDefaultItem.ChooseOne Then
                strDefaultText = GetGlobalResourceObject("Controls", "DDLItemChoose")
            Else
                Dim NothingValueItem As New RadComboBoxItem
                Dim NothingDescriptionItem As New RadComboBoxItem

                NothingValueItem.Text = GetGlobalResourceObject("Controls", "DDLItemNothing")
                NothingDescriptionItem.Text = GetGlobalResourceObject("Controls", "DDLItemNothing")

                NothingValueItem.Value = "-2" 'GetGlobalResourceObject("Controls", "DDLItemDefaultValue")
                NothingDescriptionItem.Value = "-2" 'GetGlobalResourceObject("Controls", "DDLItemDefaultValue")

                NothingValueItem.Selected = True
                NothingDescriptionItem.Selected = True

                rcbv.Items.Add(NothingValueItem)
                rcbd.Items.Add(NothingDescriptionItem)

                strDefaultText = GetGlobalResourceObject("Controls", "DDLItemAll")
            End If

            Dim DefaultValueItem As New RadComboBoxItem
            Dim DefaultDescriptionItem As New RadComboBoxItem

            DefaultValueItem.Text = strDefaultText
            DefaultDescriptionItem.Text = strDefaultText

            DefaultValueItem.Value = GetGlobalResourceObject("Controls", "DDLItemDefaultValue")
            DefaultDescriptionItem.Value = GetGlobalResourceObject("Controls", "DDLItemDefaultValue")

            If strDefaultText = GetGlobalResourceObject("Controls", "DDLItemAll") Then
                DefaultValueItem.Selected = False
                DefaultDescriptionItem.Selected = False
            Else
                DefaultValueItem.Selected = True
                DefaultDescriptionItem.Selected = True
            End If

            If value = DropDownDefaultItem.ChooseOne Then
                Dim NothingValueItem As New RadComboBoxItem
                Dim NothingDescriptionItem As New RadComboBoxItem

                NothingValueItem.Text = ""
                NothingDescriptionItem.Text = ""

                NothingValueItem.Value = "-2" 'GetGlobalResourceObject("Controls", "DDLItemDefaultValue")
                NothingDescriptionItem.Value = "-2" 'GetGlobalResourceObject("Controls", "DDLItemDefaultValue")

                NothingValueItem.Selected = False
                NothingDescriptionItem.Selected = False

                NothingValueItem.Enabled = False
                NothingDescriptionItem.Enabled = False

                rcbv.Items.Add(NothingValueItem)
                rcbd.Items.Add(NothingDescriptionItem)
            End If

            rcbv.Items.Add(DefaultValueItem)
            rcbd.Items.Add(DefaultDescriptionItem)
        End Set
    End Property

    Protected Sub rcbv_SelectedIndexChanged(ByVal o As Object, ByVal e As Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs) Handles rcbv.SelectedIndexChanged
        OnSelectedIndexChanged(e)
        ' In post back put the same value in the description DropDown List
        If rcbv.SelectedValue = "" Then
            rcbv.SelectedIndex = "-1"
            rcbd.SelectedIndex = "-1"
            Exit Sub
        End If
        rcbd.SelectedValue = rcbv.SelectedValue
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        rcbd.OnClientSelectedIndexChanged = "OCSICDesc" & rcbd.ClientID
        rcbv.OnClientSelectedIndexChanged = "OCSICVal" & rcbv.ClientID
        CVrcbv.ClientValidationFunction = "ClientValidate" & CVrcbv.ClientID
    End Sub

    Protected Sub CVrcbv_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CVrcbv.ServerValidate
        Try
            If args.Value = rcbv.FindItemByValue(GetGlobalResourceObject("Controls", "DDLItemDefaultValue")).Text Or args.Value = rcbv.FindItemByValue("-2").Text Then
                args.IsValid = False
                Return
            Else
                args.IsValid = True
                Return
            End If
        Catch ex As Exception
            args.IsValid = False
            Return
        End Try
    End Sub

    'This Code is Added ToPrevent Duplicate the recprds each time the ComboBox is rebined
    Protected Sub rcbd_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles rcbd.DataBinding
        ClearAndAddDefaultItem(rcbd)
    End Sub

    'This Code is Added ToPrevent Duplicate the recprds each time the ComboBox is rebined
    Protected Sub rcbv_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles rcbv.DataBinding
        ClearAndAddDefaultItem(rcbv)
    End Sub


    Private Sub ClearAndAddDefaultItem(ByRef comboBox As RadComboBox)
        Dim defaultItem As New RadComboBoxItem
        Dim NothingItem As New RadComboBoxItem

        NothingItem = comboBox.FindItemByValue("-2")
        defaultItem = comboBox.FindItemByValue("-1")
        comboBox.Items.Clear()

        If Not IsNothing(NothingItem) Then
            comboBox.Items.Add(New RadComboBoxItem(NothingItem.Text, NothingItem.Value))
        End If


        If Not IsNothing(defaultItem) Then
            comboBox.Items.Add(New RadComboBoxItem(defaultItem.Text, defaultItem.Value))
        End If
    End Sub

    Protected Sub rcbd_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles rcbd.DataBound
        Dim defaultItem As New RadComboBoxItem
        Dim NothingItem As New RadComboBoxItem

        NothingItem = rcbd.FindItemByValue("-2")
        defaultItem = rcbd.FindItemByValue("-1")
        If defaultItem.Text = GetGlobalResourceObject("Controls", "DDLItemChoose") Then
            NothingItem.Enabled = False
            NothingItem.Selected = False
            defaultItem.Selected = True
        Else
            NothingItem.Enabled = True
            NothingItem.Selected = True
            defaultItem.Selected = False
        End If
    End Sub


    Protected Sub rcbv_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles rcbv.DataBound
        Dim defaultItem As New RadComboBoxItem
        Dim NothingItem As New RadComboBoxItem

        NothingItem = rcbv.FindItemByValue("-2")
        defaultItem = rcbv.FindItemByValue("-1")
        If defaultItem.Text = GetGlobalResourceObject("Controls", "DDLItemChoose") Then
            NothingItem.Enabled = False
            NothingItem.Selected = False
            defaultItem.Selected = True
        Else
            NothingItem.Enabled = True
            NothingItem.Selected = True
            defaultItem.Selected = False
        End If

    End Sub
End Class
